package net.example.lesson5.lock;

import java.util.concurrent.TimeUnit;

public class SynchronizedDemo {
    private static int c = 100;
    private  static Object lock = new String();

    public static  void action0(){
        c += 50;
        System.out.println(Thread.currentThread().getName()+"  "+c);
    }
    public static  void action1(){
        synchronized(lock){
            c += 50;
        }
        System.out.println(Thread.currentThread().getName()+"  "+c);
    }
    public synchronized static  void action2(){
        c += 50;
        System.out.println(Thread.currentThread().getName()+"  "+c);
    }

    public static void main(String[] args) {
        Runnable task = ()->{
                while (true){
                    action2();
                    try {
                        TimeUnit.MILLISECONDS.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }};
        Thread thread1 = new Thread(task);
        Thread thread2 = new Thread(task);
        thread1.start();
        thread2.start();
    }
}
